<?php
/**
 * 模块：wap首页
 * @copyright (C)2011 Cenwor Inc.
 * @author Moyo <dev@uuland.org>
 * @package module
 * @name index.mod.php
 * @version 1.0
 */
class ModuleObject extends MasterObject
{
	function ModuleObject( $config )
	{
		$this->MasterObject($config);
		$runCode = Load::moduleCode($this);
		$this->$runCode();
	}
	function Main()
	{	
		$clientUser = get('u', 'int');
		if ( $clientUser != '' )
		{
			handler('cookie')->setVar('finderid', $clientUser);
			handler('cookie')->setVar('findtime', time());
		}
		
		$data = logic('product')->display();
				/* if (!$data && get('page', 'int') == 0)
		{
			header('Location: '.rewrite('?mod=subscribe&code=mail'));
			exit;
		} */
		$product = $data['product'];
		$isView = get('view', 'int');
		if ($isView) {
			$this->Title = $data['mutiView'] ? '' : $product['name'];
			$data['mutiView'] || mocod('product.view');
			$data['mutiView'] || productCurrentView($product);
			$favorited = logic('favorite')->get_one($product['id']);
			$product['content'] = $this->lazyload($product['content']);
			/* if(INDEX_DEFAULT){
				$new_product = logic('product')->GetNewList(2);
			} */
			include handler('template')->file("detail");
		}else{
			$this->Title = $data['mutiView'] ? '' : $product['name'];
			$data['mutiView'] || mocod('product.view');
			$data['mutiView'] || productCurrentView($product);
			$favorited = logic('favorite')->get_one($product['id']);
			/* if(INDEX_DEFAULT){
				$new_product = logic('product')->GetNewList(2);
			} */
			
			include handler('template')->file("index");
		}
		
		
	}
	
	function City(){
		$this->Title = '选择城市';
		include handler('template')->file("city");
	}
	
	function xiuche(){
		$city = isset($this->Get['city'])?$this->Get['city']:'';
		$lat = isset($this->Get['lat'])?$this->Get['lat']:'';
		$lon = isset($this->Get['lon'])?$this->Get['lon']:'';
		if($city&&$lat&&$lon){
			$a_b_loc = true;
		}
		$this->Title = '行骑网-便民修车点';
		include handler('template')->file("bianmin");
	}
	
	function getLocation(){
		if (!X_IS_AJAX)
		{
			header('Location: /');
			exit;
		}
		$city=isset($this->Get['city'])?$this->Get['city']:'';
		if($city){
			$cid = logic('misc')->getCityid($city);
			if ($cid) {
				handler('cookie')->setVar('mycity', $cid);
				echo $city;
			}else{
				$cid = ini('product.default_city');
				handler('cookie')->setVar('mycity', $cid);
				$cityAry = logic('misc')->CityList($cid);
				foreach ($cityAry as $i => $city){
					if ($city['cityid'] == $cid){						
						break;
					}
				}
				echo $city['cityname'];
			}
		}else{
			$cid = ini('product.default_city');
			handler('cookie')->setVar('mycity', $cid);
			$cityAry = logic('misc')->CityList($cid);
			foreach ($cityAry as $i => $city){
				if ($city['cityid'] == $cid){						
					break;
				}
			}
			echo $city['cityname'];
		}
		exit;
		
		
	}
	
	
	function getMore(){
		$catalog=isset($this->Post['catalog'])?$this->Post['catalog']:'';
		//$page = isset($this->Post['page'])?$this->Post['page']:1;
		$data = logic('product')->display(logic('catalog')->Filter($catalog));
		$temp = array();
		if (!$data)
		{
			$temp['status']=0;
			echo json_encode($temp);
		}
		else
		{	
			//print_r(json_encode($data['product']));exit;
			
			$product = (isset($data['product']['id']) && $data['product']['id']>0) ? array($data['product']) : $data['product'];
			$temp['status']=1;
			$temp['items']=$product ;
			echo json_encode($temp);
		}
	}
	
	function lazyload($content) {
		if(!is_robot()) {
			//$content=preg_replace('/<img(.+)src=[\'"]([^\'"]+)[\'"](.*)>/i',"<img\$1data-original=\"\$2\" src=\"/static/images/1x1.gif\"\$3>\n<noscript>\$0</noscript>",$content);
			$pattern="/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
			$content = preg_replace($pattern,"<img data-original=\"\$1\" src=\"/static/images/loading.gif\">",$content);
		}
		return $content;
	}
	
	function ExpressConfirm()
	{
		$oid = $this->Get['id'];
		$result = $this->OrderLogic->orderExpressConfirm($oid);
		if ( $result )
		{
			$this->Messager(__('已经确认收货，本次交易完成！'), '?mod=me&code=order');
		}
		else
		{
			$this->Messager(__('无效的订单号！'), '?mod=me&code=order');
		}
	}
}
?>